import Vue from 'vue'
import Router from 'vue-router'


const Home = () => import('../views/Home.vue')
const Login = () => import('../views/Login.vue')
const Users = () => import('../views/user/Users.vue')

Vue.use(Router)

const routes = [
  {
    path: '/',
    redirect: '/login'
  },
  {
    path: '/login',
    component: Login
  },
  {
    path: '/home',
    component: Home,
    redirect: '/users',
    children: [
      {path: '/users', component: Users}
    ]
  }
]

const router = new Router({
  routes
})

router.beforeEach((to, from, next) =>{
  if(to.path === '/login') return next()
  const tokenStr = window.sessionStorage.getItem('token')
  if(!tokenStr) return next('/login')
  next()
})
export default router
